package com.liyuan.help_e.component;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.liyuan.help_e.model.User;
import org.springframework.context.annotation.Bean;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class Utils {
    @Bean
    public SimpleDateFormat simpleDateFormat() {
        return new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
    }

    public User getUserFromToken(String authToken) {
        return null;
    }

    //TODO token目前设置为 一小时
    public String getToken(UserDetails userDetails) {
        Date start = new Date();
        long curentTime = System.currentTimeMillis() + 1000* 60 *60 * 10;
        Date end = new Date(curentTime);
        String token = JWT.create().withAudience(userDetails.getUsername())
                .withIssuedAt(start)
                .withExpiresAt(end)
                .sign(Algorithm.HMAC256(userDetails.getPassword()));
        return token;
    }
}
